
using System;
using System.Reflection;

using GoogleMobileAds.Common;

namespace GoogleMobileAds.Api
{
    public class InterstitialAd
    {
        private IInterstitialClient client;

        // Creates an InterstitialAd.
        public InterstitialAd(string adUnitId)
        {
            Type googleMobileAdsClientFactory = Type.GetType(
                "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp");
            MethodInfo method = googleMobileAdsClientFactory.GetMethod(
                "BuildInterstitialClient",
                BindingFlags.Static | BindingFlags.Public);
            this.client = (IInterstitialClient)method.Invoke(null, null);
            client.CreateInterstitialAd(adUnitId);

            this.client.OnAdLoaded += (sender, args) =>
                {
                    if(this.OnAdLoaded != null)
                    {
                        this.OnAdLoaded(this, args);
                    }
                };

            this.client.OnAdFailedToLoad += (sender, args) =>
                {
                    if(this.OnAdFailedToLoad != null)
                    {
                        this.OnAdFailedToLoad(this, args);
                    }
                };

            this.client.OnAdOpening += (sender, args) =>
                {
                    if(this.OnAdOpening != null)
                    {
                        this.OnAdOpening(this, args);
                    }
                };

            this.client.OnAdClosed += (sender, args) =>
                {
                    if(this.OnAdClosed != null)
                    {
                        this.OnAdClosed(this, args);
                    }
                };

            this.client.OnAdLeavingApplication += (sender, args) =>
                {
                    if(this.OnAdLeavingApplication != null)
                    {
                        this.OnAdLeavingApplication(this, args);
                    }
                };
        }

        // These are the ad callback events that can be hooked into.
        public event EventHandler<EventArgs> OnAdLoaded;

        public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;

        public event EventHandler<EventArgs> OnAdOpening;

        public event EventHandler<EventArgs> OnAdClosed;

        public event EventHandler<EventArgs> OnAdLeavingApplication;

        // Loads an InterstitialAd.
        public void LoadAd(AdRequest request)
        {
            client.LoadAd(request);
        }

        // Determines whether the InterstitialAd has loaded.
        public bool IsLoaded()
        {
            return client.IsLoaded();
        }

        // Displays the InterstitialAd.
        public void Show()
        {
            client.ShowInterstitial();
        }

        // Destroys the InterstitialAd.
        public void Destroy()
        {
            client.DestroyInterstitial();
        }
    }
}
